package com.yunhe.sms.controller;

import com.aliyuncs.exceptions.ClientException;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.commens.util.MathCode;
import com.yunhe.commens.util.ValidatePcode;
import com.yunhe.commens.util.mail.Mail;
import com.yunhe.commens.util.mail.MailInit;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.UserService;
import com.yunhe.sms.service.system.impl.RoleServiceImpl;
import com.yunhe.sms.service.system.impl.UserServiceImpl;
import com.yunhe.webmvc.annocation.RequestBean;
import com.yunhe.webmvc.annocation.RequestMapping;
import com.yunhe.webmvc.annocation.ResponseJSON;
import com.yunhe.webmvc.mode.JsonResponce;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/25 0025 12:38
 * @Description: 用户的业务处理
 */
@RequestMapping("/user")
public class UserController {
    private UserService userService = new UserServiceImpl ();
    private RoleService roleService =new RoleServiceImpl ();

    /**
     * 列表
     * @return
     */
    @RequestMapping
    public String list(){
        return "system/user/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest req){
        List<Role> list = roleService.findListStatu ();
        req.setAttribute ( "listRole",list );
        return "system/user/add";
    }
    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req){
        User user = userService.findOne ( id );//查询当前用户信息
        List<Role> list = roleService.findListStatu ();//查询角色信息
        req.setAttribute ( "listRole",list );
        req.setAttribute ( "user",user );
        return "system/user/edit";
    }

    @RequestMapping("/deleted")
    @ResponseJSON
    public JsonResponce deleted(String ids){
        userService.deleted ( ids );
        return new JsonResponce();
    }

    @RequestMapping("/save")
    public String save(@RequestBean User user) throws ClientException, GeneralSecurityException, IOException, MessagingException {
        Map<String,Object> map=new HashMap<> ();
        userService.save ( user );
        map.put ( "name",user.getUserName () );
        map.put ( "password",user.getPassword () );
        ValidatePcode.send ( user.getPhone (),map );
        Mail mail = new Mail ( "zhaofeiyang@qq.com",user.getMail (),"你好","注册成功,初始密码为"+user.getPassword ()+",请及时登录修改密码！");
        MailInit.mail (mail );
        return "redirect:/user";
    }

    @RequestMapping("/update")
    public String update(@RequestBean User user){
        userService.update ( user );
        return "redirect:/user";
    }

    @RequestMapping("/page")
    @ResponseJSON
    public Pagedao<User> page(int offset,int limit, HttpServletResponse resp ){
        resp.setCharacterEncoding ( "utf-8" );
        Pagedao<User> page = userService.findPage ( offset, limit );
        return page;
    }

    @RequestMapping("/verifyname")
    @ResponseJSON
    public JsonResponce verifyname(String name){
        User user = userService.user ( name );
        Boolean verify=false;
        if (null !=user){
            verify=true;
        }
        return new JsonResponce(1,"",verify);
    }

    @RequestMapping("/changepwd")
    public String changepwd(){
        return "system/user/changepwd";
    }

    @RequestMapping("/updatepwd")
    @ResponseJSON
    public JsonResponce updatepwd(String oldpassword,String password1,String password2, HttpServletRequest req,HttpServletResponse resp){
        resp.setCharacterEncoding ( "utf-8" );
        HttpSession session = req.getSession ();
        String userName = (String)session.getAttribute ( "userName" );
        User user = userService.user ( userName );
        String msg="";
        int code=0;
        if (null != user  && !user.getPassword ().equals ( oldpassword ) ){
             msg="密码输入错误！";
        }else if(oldpassword.equals ( password1 )){
            msg="新密码与原密码不能相同！";
        }else if (!password1.equals ( password2 )){
            msg="两次输入的密码不一致";
        }else{
            userService.updatepwd ( password1,userName );
            code=1;
        }
        return new JsonResponce ( code,msg );
    }
}
